package model.wrap.structure;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import security.Security;

import model.entityes.exception.AllStructuresExeption;
import model.forview.StructureCreateModelForView;
import model.wrap.node.Node;
import model.wrap.user.User;
import model.wrap.user.UserException;
import models.AllStructures;

public class Structure
{

    protected AllStructures structureLocal;

    public Structure(StructureCreateModelForView form)
    {
	this.structureLocal = new AllStructures();
	setDescription(form.description);
	setTitle(form.title);
    }

    public Structure(Map<String, String> form)
    {
	String description = "", title = "";
	Long id = new Long(0);

	Iterator<String> keys = form.keySet().iterator();
	while (keys.hasNext())
	{
	    String key = keys.next();
	    if (key.equals("data[description]")) description = form.get(key);
	    if (key.equals("data[title]")) title = form.get(key);
	    if (key.equals("data[id]")) id = Long.valueOf(form.get(key));
	}

	try
	{
	    this.structureLocal = AllStructures.getStructure(id);
	}
	catch (AllStructuresExeption e)
	{
	    e.printStackTrace();
	}
	if (null == this.structureLocal) this.structureLocal = new AllStructures();

	setDescription(description);
	setTitle(title);
    }

    public Structure(Long id)
    {
	try
	{
	    this.structureLocal = AllStructures.getStructure(id);
	}
	catch (AllStructuresExeption e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    this.structureLocal = new AllStructures();
	}
    }

    public Structure(AllStructures str)
    {
	this.structureLocal = str;
    }

    public Structure()
    {
	this.structureLocal = new AllStructures();
    }

    public long getId()
    {
	return structureLocal.getId();
    }

    public void setId(long id)
    {
	structureLocal.setId(id);
    }

    public String getTitle()
    {
	return structureLocal.getTitle();
    }

    public void setTitle(String title)
    {
	structureLocal.setTitle(title);
    }

    public String getDescription()
    {
	return structureLocal.getDescription();
    }

    public void setDescription(String description)
    {
	structureLocal.setDescription(description);
    }

    public User getCreater() throws StructureException
    {
	User user;
	try
	{
	    user = new User(structureLocal.getCreater());
	    return user;
	}
	catch (UserException e)
	{
	    // TODO
	    // e.printStackTrace();
	    throw StructureException.errorCreaterNotFind();
	}
    }

    public void save()
    {
	this.setCreater(Security.getUserFromSession());
	this.structureLocal.save();
    }

    public void update()
    {
	this.setCreater(Security.getUserFromSession());
	this.structureLocal.update();
    }

    public void delete()
    {
	ArrayList<Node> nodes = Node.getAllNodes(this);
	for (Node node : nodes)
	{
	    node.delete();
	}
	this.structureLocal.delete();
    }

    public void setCreater(User user)
    {
	structureLocal.setCreater(user.getEmail());
    }

    /********************************************************/

    public static ArrayList<Structure> getAllStructures()
    {
	List<AllStructures> allStr = AllStructures.find.all();
	ArrayList<Structure> strs = new ArrayList<Structure>();

	for (AllStructures allStructures : allStr)
	{
	    strs.add(new Structure(allStructures));
	}

	return strs;
    }
    
    public static ArrayList<Structure> getStructuresByFilter(String query)
    {
	List<AllStructures> allStr = AllStructures.getStructuresByFilter(query);
	ArrayList<Structure> strs = new ArrayList<Structure>();

	for (AllStructures allStructures : allStr)
	{
	    strs.add(new Structure(allStructures));
	}

	return strs;
    }
    
    public int getSizeOfStructuresNode()
    {
	return Node.getAllNodes(this).size();
    }
}
